/*
 * DisconnectionMessageHandler.java    ver   03/gen/2010
 *
 * Copyright (c) 2009 mentalsmash.org
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * The use of the Apache License does not indicate that this project is
 * affiliated with the Apache Software Foundation.
 */
package org.mentalsmash.tazio.transparency.impl;

import java.io.Serializable;

import org.mentalsmash.tazio.commons.identifiers.NID;
import org.mentalsmash.tazio.transparency.commons.messages.DisconnectionMessage;
import org.mentalsmash.tazio.transparency.interfaces.InputMessageListener;
import org.mentalsmash.tazio.transparency.interfaces.Resolver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.inject.internal.Preconditions;

/**
 * @version ver 03/gen/2010
 * @author Andrea Reale <andrea.reale@gmail.com>
 * 
 */
class DisconnectionMessageHandler implements InputMessageListener {

    private final static Logger log = LoggerFactory.getLogger(DisconnectionMessageHandler.class);
    private final Resolver resolver;
    
    public DisconnectionMessageHandler(Resolver resolver) {
	this.resolver = resolver;
    }
    /*
     * (non-Javadoc)
     * 
     * @seeorg.mentalsmash.tazio.transparency.interfaces.InputMessageListener#
     * handleMessage(org.mentalsmash.tazio.commons.identifiers.NID,
     * java.io.Serializable)
     */
    @Override
    public <T extends Serializable> Serializable handleMessage(NID nid,
	    T message) {
	Preconditions.checkNotNull(message);
	Preconditions.checkArgument(message instanceof DisconnectionMessage);
	DisconnectionMessage msg = (DisconnectionMessage) message;

	log.debug("Received a disconnection message from {}", msg
		.getDisconnectedNode());
	
	resolver.removeMapping(msg.getDisconnectedNode());

	return null;
    }

}